<script setup lang="ts">
import { computed, ref, withDefaults } from 'vue';
import { useWorkflowStore } from '@/store/modules/workflow';
import Node from './Node.vue';

type ValidationType = 1;

defineOptions({
  name: 'ApprovalNode'
});

const props = withDefaults(defineProps<{ config: any }>(), {
  config: () => {
    return {};
  }
});
const workflowStore = useWorkflowStore();

const showError = ref<boolean>(false);
const errorInfo = ref<string>('');

const checkError = (validationType: ValidationType, err) => {
  switch (validationType) {
    case 1:
      if (props.config.nodeAssigneeList.length > 0) {
        return true;
      }
      errorInfo.value = '请指定审批人员';
      err.push(`${props.config.nodeName} 未指定审批人员`);
      return false;

    default:
      // Handle unknown validation types
      return false;
  }
};

// 校验数据配置的合法性
const validate = err => {
  try {
    showError.value = !checkError(props.config.setType, err);
    return showError.value;
  } catch (e) {
    return true;
  }
};
const headerBgc = computed(() => {
  if (workflowStore.diagramMode === 'viewer') {
    return props.config.headerBgc as string;
  }
  return '#ff943e';
});
const content = computed(() => {
  const config = props.config;
  switch (config.setType) {
    case 1:
      if (config.nodeAssigneeList.length > 0) {
        const texts: any = [];
        config.nodeAssigneeList.forEach((org: any) => texts.push(org.name));
        return String(texts).replaceAll(',', '、');
      }
      return '请指定审批人';

    default:
      return '未知设置项';
  }
});

defineExpose({
  validate
});
</script>

<template>
  <Node
    :title="config.nodeName"
    :show-error="showError"
    :content="content"
    :error-info="errorInfo"
    placeholder="请设置审批人"
    :header-bgc="headerBgc"
    header-icon="x-icon-seal"
  />
</template>

<style scoped></style>
